import { pipe, sort } from 'ramda';
import { Coupon } from '../api/spu/getOrderCoupons';
import { CouponType } from '../pages/CouponList/constant';

export const empty = '';

export interface CouponRes {
  selectedCouponList: Coupon[];
  validCouponNumber: number;
}

export interface CouponValidList {
  couponList: Coupon[];
}

// 提取限制productId对应商品的优惠券列表 && 价格排序
const getLimitCouponList = (
  productId: number,
  payAmount: number
): ((list: Coupon[]) => Coupon[]) =>
  pipe(
    (list: Coupon[]): Coupon[] =>
      list.filter(
        (item: Coupon): boolean =>
          !item.noLimitProduct &&
          !!item.useProducts.find(
            (product): boolean => product.id === productId
          ) &&
          payAmount >= (item.leastAmount ? item.leastAmount : 0)
      ),
    sort((left, right): number => right.amount - left.amount)
  );

// 全场通用的优惠券列表 && 价格排序
const getNoLimitCouponList = (
  payAmount: number
): ((list: Coupon[]) => Coupon[]) =>
  pipe(
    (list: Coupon[]): Coupon[] =>
      list.filter(
        (item: Coupon): boolean =>
          item.noLimitProduct &&
          payAmount >= (item.leastAmount ? item.leastAmount : 0)
      ),
    sort((left, right): number => right.amount - left.amount)
  );

// 用户选择限制商品的优惠券，需要计算当前还有哪些全场通用的优惠券是可用的
export const getValidCouponList = (
  productId: number, // spu id
  payAmount: number, // 需要支付金额（参与优惠券优惠的金额）
  coupons: Coupon[], // 优惠券列表（未过期，未使用的）
  selectedLimitCoupon?: Coupon | null // 当前选择的限制商品的优惠券
): CouponValidList => {
  const limitCouponList = getLimitCouponList(productId, payAmount)(coupons);

  // 使用完限制优惠券后的价格，用于提取可用 全场通用优惠券
  const resAmount =
    payAmount - (selectedLimitCoupon ? selectedLimitCoupon.amount : 0);

  const noLimitCouponList = getNoLimitCouponList(resAmount)(coupons);

  const couponList = coupons.map(
    (item): Coupon => {
      const canUse =
        !!limitCouponList.find((product): boolean => product.id === item.id) ||
        !!noLimitCouponList.find((product): boolean => product.id === item.id);
      return {
        ...item,
        canUse,
        status: canUse ? CouponType.Normal : CouponType.Expired
      };
    }
  );
  return { couponList };
};

export const getInitCouponInfo = (
  productId: number, // spu id
  payAmount: number, // 需要支付金额（参与优惠券优惠的金额）
  coupons: Coupon[] // 优惠券列表（未过期，未使用的）
): CouponRes => {
  let selectedCouponList: Coupon[] = [];

  const limitCouponList = getLimitCouponList(productId, payAmount)(coupons);

  // 如果存在限制优惠券，默认取第一个优惠券
  if (limitCouponList.length > 0) {
    selectedCouponList = [limitCouponList[0]];
  }

  // 使用完限制优惠券后的价格，用于提取可用 全场通用优惠券
  const resAmount =
    payAmount -
    (selectedCouponList.length > 0 ? selectedCouponList[0].amount : 0);

  const noLimitCouponList = getNoLimitCouponList(resAmount)(coupons);

  const allNoLimitCouponList = getNoLimitCouponList(payAmount)(coupons);

  // 如果没有限制优惠券，且存在全场通用优惠券，默认取第一个优惠券
  if (noLimitCouponList.length > 0) {
    selectedCouponList = [...selectedCouponList, noLimitCouponList[0]];
  }

  return {
    selectedCouponList,
    validCouponNumber: limitCouponList.length + allNoLimitCouponList.length
  };
};
